package br.usp.ime.protoc.client.view;

import com.google.gwt.core.client.JavaScriptObject;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridRowListenerAdapter;
import com.gwtext.client.widgets.layout.FitLayout;

public class PacientListView extends Panel {
	
	private GridPanel grid = new GridPanel();
	
	public PacientListView() {
		this.setLayout(new FitLayout());
		this.setBodyBorder(false);
	
		//this.add(label);

		RecordDef recordDef = new RecordDef(new FieldDef[] {
				new IntegerFieldDef("id"), 
				new StringFieldDef("nome"), 
				new StringFieldDef("cpf"),
				new StringFieldDef("fone"), 
				new StringFieldDef("email") });  
				   
		Object[][] data = getPacienteData();
		MemoryProxy proxy = new MemoryProxy(data);

		ArrayReader reader = new ArrayReader(recordDef);
		Store store = new Store(proxy, reader);
		store.load();
		grid.setStore(store);  
		
	    ColumnModel columnModel = new ColumnModel(new ColumnConfig[] {
				new ColumnConfig("Nome", "nome", 200, true),
				new ColumnConfig("CPF", "cpf", 100, true),
				new ColumnConfig("Telefone", "fone", 75, true),
				new ColumnConfig("Email", "email", 150, true)});  
				   
		
	    grid.setColumnModel(columnModel);

		//grid.setBorder(false);
		grid.setFrame(true);
		grid.setStripeRows(true);
		grid.setTitle("Lista de Pacientes");
		//grid.setAutoExpandColumn("nome");  
	       	    
	    //grid.setWidth(400);
	    grid.setHeight(MainView.HEIGHT-200);
	    grid.addGridRowListener(new GridRowListenerAdapter() {

			@Override
			public void onRowDblClick(GridPanel grid, int rowIndex,
					EventObject e) {
				Store store=grid.getStore();
				Record record=store.getAt(rowIndex);
				record.getAsInteger("id");
				showEditWindow(rowIndex);
				super.onRowDblClick(grid, rowIndex, e);
			}
	    });
		//Viewport viewport = new Viewport(grid);
		this.add(grid);
		//initWidget(panel);
	}

	public void showEditWindow(int pacientID) {
		final Window win=new Window();
		win.setTitle("Edit");
		win.setCloseAction(Window.CLOSE);
		win.add(new PacientEditView(pacientID));
		//win.setWidth(450);
		//win.setHeight(400);
		win.show();
	}
	
	public Object[][] getPacienteData() {
		return new Object[][]{  
				new Object[] {new Integer(0),"Seu Madruga","000.000.000-00","1406","madruga@televisa.mx"},
				new Object[] {new Integer(1),"Dona Florinda","000.000.001-79","3785-6666","florinda@televisa.mx"},
		};
	}
	
	@Override
	protected JavaScriptObject create(JavaScriptObject config) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected JavaScriptObject getConfigPrototype() {
		// TODO Auto-generated method stub
		return null;
	}
}
